package com.bjpowernode.controller;

import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description
 * @Author hhwu
 * @Date 2022/3/14 10:15
 * Version 1.0
 **/

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService service;

    //注册学生
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student) {
        ModelAndView mv = new ModelAndView();
        String tips = "注册失败";
        int num = service.addStudent(student);
        if (num > 0) {
            //注册成功
            tips = "学生[" + student.getName() + "]注册成功";
        }
        mv.addObject("tips", tips);
        mv.setViewName("result");
        return mv;
    }

    //处理查询，相应ajax
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List<Student> questStudent() {
        List<Student> students = service.findStudent();
        return students;
    }

    @ResponseBody
    @RequestMapping("/findStudentById.do")
    public Student findStudentById(int id) {
        System.out.println("lll");
        Student student = service.findStudentById(id);
        System.out.println(student);
        return student;
    }
}
